import 'dart:convert';

import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:szwf/page/out/index.dart';
import 'package:szwf/page/put/index.dart';
import 'package:szwf/utils/common_toast.dart';
import 'package:szwf/utils/dio_http.dart';

class BarCodeScanPage extends StatefulWidget {
  BarCodeScanPage({Key key}) : super(key: key);

  @override
  _BarCodeScanPageState createState() => _BarCodeScanPageState();
}

class _BarCodeScanPageState extends State<BarCodeScanPage> {

  initState() {
    super.initState();
    scan();//进入页面直接扫码
  }


  var _content= TextEditingController();
  final _formKey = GlobalKey<FormState>();
  String qrNumber;
  String type;
  scan() async {
    var options = ScanOptions(
      strings: {
        "cancel": "关闭",
        "flash_on": "开灯",
        "flash_off": "关灯",
      },
    );

    ScanResult result = await BarcodeScanner.scan(options: options);
    setState(() {
      _content.text = result.rawContent;
    });

    onSave(type);
  }

  onSave (String type) async {
      //成功
//      CommonToast.showToast('正在查询!');
      if(type == '出库'){
        //出库
        const url = '/wfgl/entrancewarehouse/findOneByQrNumber';
        var params = {
          "qrNumber":_content.text,
        };
        var res = await DioHttp.of(context).get(url,params);
        var resString = json.decode(res.toString());
        print(resString);
        if(resString['rc'] == 0){
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (context) {
                return OutPage();
              },
              settings: RouteSettings(
                arguments: resString['ret'],
              ), // 传参
              fullscreenDialog: true,
            ),
          );
        }else{
          CommonToast.showToast(resString['err']);
          Navigator.of(context)..pop();

        }

      }else{
        //入库
        const url = '/wfgl/qrcode/findOneByQrNumber';
        var params = {
          "qrNumber":_content.text,
        };
        var res = await DioHttp.of(context).get(url,params);
        var resString = json.decode(res.toString());

        if(resString['rc'] == 0){
          var data = resString['ret'];
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (context) {
                return PutPage();
              },
              settings: RouteSettings(
                arguments: data,
              ), // 传参
              fullscreenDialog: true,
            ),
          );
        }else{
          CommonToast.showToast(resString['err']);
          Navigator.of(context)..pop();

        }

      }
  }

  @override
  Widget build(BuildContext context) {
    Map typePage = ModalRoute.of(context).settings.arguments;
    setState(() {
      type = typePage["name"];
    });
    return Scaffold(
        resizeToAvoidBottomInset:false,
      appBar: AppBar(
        title: Center(
          child: Text('扫码${typePage["name"]}'),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.camera_alt),
            onPressed: () {
              scan();
            },
          ),
        ],
      ),
    );
  }

}